package com.colorful.insuranceproject.request;

import android.os.Handler;

import com.colorful.insuranceproject.adapter.CityListBean;
import com.colorful.insuranceproject.bean.AliPayBean;
import com.colorful.insuranceproject.bean.ConsultBean;
import com.colorful.insuranceproject.bean.HealthInfoBean;
import com.colorful.insuranceproject.bean.HomeBean;
import com.colorful.insuranceproject.bean.PaySuccessBean;
import com.colorful.insuranceproject.bean.WeChatPayBean;
import com.colorful.insuranceproject.bean.WebViewBean;
import com.colorful.insuranceproject.consts.URLConst;
import com.lovely3x.common.utils.ALog;
import com.lovely3x.common.utils.Response;
import com.lovely3x.jsonparser.model.JSONObject;

import okhttp3.OkHttpClient;

/**
 * Created by handsome-Bob on 2017/10/27.
 * <p>
 * 首页数据请求器
 */

public class HomeRequest extends BaseRequest {
    public HomeRequest(Handler handler) {
        super(handler);
    }


    /**
     * 获取首页数据
     *
     * @param what handler传递值
     */
    public void getHomePageData(final int page, int what) {
        process(new ProcessResultExtension() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "获取首页数据展示-------------->" + result);
                    res.obj = result.getJSONObject(DATA_KEY).createObject(HomeBean.class);
                }
            }

            @Override
            public void onRequestBefore(Response res, OkHttpClient client) {
                res.addtional = page;
            }
        }, URLConst.GET_HOME_PAGE_DATA, what, "page", page);
    }

    /**
     * 获取咨询页面的数据
     *
     * @param page              页码
     * @param getConsultListUrl handler传递值
     */
    public void getConsultPageData(final int page, int getConsultListUrl) {
        process(new ProcessResultExtension() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "获取咨询页面的数据---------------->" + result);
                    res.obj = result.getJSONArray(DATA_KEY).createObjects(HealthInfoBean.class);
                }
            }

            @Override
            public void onRequestBefore(Response res, OkHttpClient client) {
                res.addtional = page;
            }
        }, URLConst.GET_CONSULT_PAGE_DATA, getConsultListUrl, "page", page, "pageLength", 10);

    }

    /**
     * 获取资讯详情
     *
     * @param type id
     * @param what handler传递值
     */
    public void getConsultDetails(String type, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    res.obj = result.getJSONObject(DATA_KEY).createObject(ConsultBean.class);
                    ALog.e(TAG, "获取资讯详情----------------->" + result);
                }
            }
        }, URLConst.GET_CONSULT_DETAILS, what, "articleId", type);
    }

    /**
     * 获取咨询页面的banner
     *
     * @param what handler what
     */
    public void getConsultBanner(int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    res.obj = result.getJSONArray(DATA_KEY).createObjects(String.class);
                    ALog.e(TAG, "获取咨询页面的banner------------------>" + result);
                }
            }
        }, URLConst.GET_CONSULT_BANNER, what);
    }

    /**
     * 获取服务页面的banner
     *
     * @param what hanlder what
     */
    public void getServerPageBanner(int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    res.obj = result.getJSONArray(DATA_KEY).createObjects(String.class);
                    ALog.e(TAG, "获取服务页面的banner------------------>" + result);
                }
            }
        }, URLConst.GET_SERVER_BANNER, what);
    }

    /**
     * 获取当前城市
     *
     * @param latitude  经度
     * @param longitude 纬度
     * @param what      handler
     */
    public void getCurrentAddr(double latitude, double longitude, int what) {
        process(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "获取当前城市------------------------》" + result);
                    res.obj = result.getJSONObject(DATA_KEY).createObject(CityListBean.class);
                }
            }
        }, URLConst.CURRENT_ADDRESS_URL, what, "lat", latitude, "lng", longitude);
    }

    /**
     * 可乐否 生成订单  关于WebView展示页面数据
     *
     * @param urls 地址
     * @param what handler
     */
    public void generateWebOrderUrl(String urls, String isBatch, int what) {
        processNotUrlRequest(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                ALog.e(TAG, "可乐否 生成订单  关于WebView展示页面数据-----------------》" + result);
                res.obj = result.getJSONObject(DATA_KEY).createObject(WebViewBean.class);
            }
        }, URLConst.GENERATE_WEB_ORDER_URL, what, "orderNo", urls, "isBatch", isBatch);
    }

    /**
     * 判断订单是否支付成功了
     *
     * @param mergeNo  订单号
     * @param orderSrc 0:商城 1:微信 2:手机版 3:安卓App 4:苹果App
     * @param what     handler传递值
     */
    public void makeSureTheOrderPaymentSuccessful(String mergeNo, String orderId, String orderSrc, int payfrom, int what) {
        processNotUrlRequest(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "判断订单是否支付成功了------------------>" + result);
                    res.obj = result.getJSONObject(DATA_KEY).createObject(PaySuccessBean.class);
                }
            }
        }, URLConst.MAKE_SURE_THE_ORDER_PAYMENT_SUCCESSFUL, what, "orderId", orderId, "mergeNo", mergeNo, "orderSrc", orderSrc, "payFrom", String.valueOf(payfrom));
    }

    /**
     * 获取支付宝的信息
     *
     * @param orderNo 订单号
     * @param isBatch 订单类型
     * @param what    handler传递至
     */
    public void getAliPayData(String orderNo, int isBatch, String tokenId, int what) {
        processNotUrlRequest(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "获取支付宝支付的信息------------------》" + result);
                    res.obj = result.getJSONObject(DATA_KEY).createObject(AliPayBean.class);
                }
            }
        }, URLConst.GET_ALI_PAY_DATA, what, "orderNo", orderNo, "isBatch", isBatch/*,"tokenId",tokenId*/);
    }


    /**
     * 获取微信支付的信息
     *
     * @param mergeNo 订单号
     * @param isBatch 判断订单支付类型
     * @param what    handler传递值
     */
    public void getWeChatPayData(String mergeNo, int isBatch, int what) {
        processNotUrlRequest(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    ALog.e(TAG, "获取微信支付的信息------------------》" + result);
                    res.obj = result.getJSONArray(DATA_KEY).createObjects(WeChatPayBean.class);
                }
            }
        }, URLConst.GET_WE_CHAT_PAY_DATA, what, "orderNo", mergeNo, "isBatch", isBatch/*,"tokenId",tokenId*/);
    }

    /**
     * 可康安支付
     *
     * @param orderunique 订单号
     * @param pwd         支付密码
     * @param what        handler传递值
     */
    public void payOfKeKangAn(String orderunique, String pwd,String tokenId, int what) {
        processNotUrlRequest(new ProcessResult() {
            @Override
            public void onResult(Response res, JSONObject result) throws Exception {
                if (res.isSuccessful) {
                    res.obj = result.getJSONObject(DATA_KEY).createObject(PaySuccessBean.class);
                    ALog.e(TAG, "可康安支付---------------->" + result);
                }
            }
        }, URLConst.PAY_OF_KEKANGAN_URL, what, "orderunique", orderunique, "payPwd", pwd/*,"tokenId",tokenId*/);
    }
}
